package com.bookmanagement.service.impl;

import com.bookmanagement.entity.Category;
import com.bookmanagement.exception.BusinessException;
import com.bookmanagement.mapper.CategoryMapper;
import com.bookmanagement.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分类服务实现类
 * @author skyjay
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    
    @Autowired
    private CategoryMapper categoryMapper;
    
    @Override
    public List<Category> getAllCategories() {
        return categoryMapper.getAllCategories();
    }
    
    @Override
    public void addCategory(String name) {
        // 检查分类是否已存在
        Category existCategory = categoryMapper.getCategoryByName(name);
        if (existCategory != null) {
            throw new BusinessException("分类已存在");
        }
        
        Category category = Category.builder()
                .name(name)
                .build();
        categoryMapper.insertCategory(category);
    }
}
